package TestDemo;

/**
 * 试验java方法的传递是值传递还是引用传递
 */
public class PassValue
{
    static void exchange(int a, int b)
    {// 静态方法，交换a,b的值
        int temp;
        temp = a;
        a = b;
        b = temp;
    }

    public static void main(String[] args)
    {
        int i = 10;
        int j = 100;
        System.out.println("before call: " + "i=" + i + ", " + "j = " + j);// 调用前
        exchange(i, j); // 值传递，main方法只能调用静态方法
        System.out.println("after call: " + "i=" + i + ", " + "j = " + j);// 调用后
    }
}

class Book
{
    String name;
    private float price;
    Book(String n,float p)
    {//构造方法
        name = n;
        price = p;
    }

    static void change(Book a_book, String n, float p)
    { // 静态方法，对象作为参数
        a_book.name = n;
        a_book.price = p;
    }

    public void output()
    { // 实例方法，输出对象信息
        System.out.println("name: " + name + ", " + "price: " + price);
    }
}

class PassAddr
{
    public static void main(String[] args)
    {
        Book b = new Book("java2", 32.5f);
        System.out.print("before call: "); // 调用前
        b.output();

        //b.change(b, "c++", 45.5f);//这样也Ok,但是提示：The static method change
        //(Book, String, float)from the type Book should be accessed in a static way
        Book.change(b, "c++", 45.5f); // 引用传递，传递对象b的引用，修改对象b的值
        System.out.print("after call: "); // 调用后
        b.output();
    }
}
/*
这是基本类型，传值---传递基本数据类型参数
before call: i=10, j = 100
after call: i=10, j = 100
*/
